package com.yimi.yinhepda.db.dao;

import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.stmt.Where;
import com.yimi.yinhepda.base.BaseApplication;
import com.yimi.yinhepda.db.helper.DbHelper;
import com.yimi.yinhepda.db.table.UserInfoTable;
import com.yimi.yinhepda.entity.bean.UserInfoEntity;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户信息表操作类
 *
 * @author lj
 */
public class UserInfoTableDao extends BaseDao<UserInfoTable, Long> {

    public UserInfoTableDao() {
        super(DbHelper.getInstance(BaseApplication.getContext()), UserInfoTable.class);
    }

    public void createOrUpdate(List<UserInfoEntity> userInfoEntityList) {
        deleteAll();
        if (userInfoEntityList != null) {
            List<UserInfoTable> lists = new ArrayList<>();
            for (UserInfoEntity entity : userInfoEntityList) {
                UserInfoTable userInfoTable = new UserInfoTable();
                userInfoTable.setEmpId(entity.getEmpId());
                userInfoTable.setWorkNum(entity.getWorkNum());
                userInfoTable.setUserName(entity.getUserName());
                userInfoTable.setUserType(entity.getUserType());
                userInfoTable.setUserSex(entity.getUserSex());
                userInfoTable.setSiteCode(entity.getSiteCode());
                userInfoTable.setSiteName(entity.getSiteName());
                userInfoTable.setIsCommision(entity.getIsCommision());
                userInfoTable.setJob(entity.getJob());
                userInfoTable.setOpenAccountName(entity.getOpenAccountName());
                userInfoTable.setOpenAccountNum(entity.getOpenAccountNum());
                userInfoTable.setOpenBank(entity.getOpenBank());
                userInfoTable.setPhoneNum(entity.getPhoneNum());
                userInfoTable.setAddress(entity.getAddress());
                userInfoTable.setCompCode(entity.getCompCode());
                userInfoTable.setUserId(entity.getUserId());
                userInfoTable.setUserCode(entity.getUserCode());
                userInfoTable.setPassword(entity.getPassword());
                userInfoTable.setIsEffective(entity.getIsEffective());
                userInfoTable.setUmodifiertime(entity.getUmodifiertime());
                userInfoTable.setUmodifier(entity.getUmodifier());
                userInfoTable.setUcreater(entity.getUcreater());
                userInfoTable.setUcreatertime(entity.getUcreatertime());
                userInfoTable.setUlatesttime(entity.getUlatesttime());
                userInfoTable.setUrecordversion(entity.getUrecordversion());
                userInfoTable.setUcompcode(entity.getUcompcode());
                lists.add(userInfoTable);
            }
            try {
                insert(lists);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 根据工号查询数据
     *
     * @param workNum
     */
    public UserInfoTable getDataByWorkNum(String workNum) {
        try {
            QueryBuilder<UserInfoTable, Long> queryBuilder = dao.queryBuilder();
            Where where = queryBuilder.where();
            where.eq("workNum", workNum);
            return queryBuilder.queryForFirst();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 根据用户名查询数据
     *
     * @param userName
     */
    public UserInfoTable getDataByUserName(String userName) {
        try {
            QueryBuilder<UserInfoTable, Long> queryBuilder = dao.queryBuilder();
            Where where = queryBuilder.where();
            where.eq("userName", userName);
            return queryBuilder.queryForFirst();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public List<UserInfoTable> getDataByUserNameAndWorkNum(String joinWorkNum, String joinName) {
        try {
            QueryBuilder<UserInfoTable, Long> queryBuilder = dao.queryBuilder();
            Where where = queryBuilder.where();
            where.like("workNum", "%" + joinWorkNum + "%");
            where.and().like("userName", "%" + joinName + "%");
            return queryBuilder.query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
